package com.lecing.data.handler;

import com.lecing.data.entity.Community;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;

/**
 * Created by Administrator on 2018/5/10.
 */
public class FetchDataUtil {
    private FetchDataUtil() {
    }
    public static  FetchDataUtil getFetchDataUtil(){
        String url="https://xa.anjuke.com/community/?from=esf_list_navigation";
        String html=pickData(url);
       // Community community=analyzeHTMLByString(html);
        //return community;
        analyzeHTMLByString(html);
        return null;
    }

    /**
     * 爬取网页信息
     * @param url
     * @return
     */
    private static String pickData(String url){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                // 打印响应状态
                if (entity != null) {
                    return EntityUtils.toString(entity);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    private static void analyzeHTMLByString(String html){
        Document document= Jsoup.parse(html);
        Elements element=document.getElementsByAttribute("bot-tag");
      //  String dochtml=document.getAllElements();
        System.out.print("解析html："+element);

    }
    /**
    private static Community analyzeHTMLByString(String html){
        Document document= Jsoup.parse(html);
        String dochtml=document.outerHtml();
        System.out.print("解析html："+dochtml);
        return  null;
    }
     */

}
